package com.example.svntest;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class Activity2 extends Activity {
	
	private static final String TAG = "Activity2";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        
        findViewById(R.id.btn_startSvc).setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Activity2.this.startService(new Intent(Activity2.this, MyService.class));
			}
		});
        
        findViewById(R.id.btn_stopSvc).setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Activity2.this.stopService(new Intent(Activity2.this, MyService.class));
			}
		});
        
        findViewById(R.id.btn_bindSvc).setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Activity2.this.
				getApplicationContext(). // bug
				bindService(new Intent(Activity2.this, MyService.class),
						conn, Context.BIND_AUTO_CREATE);
			}
		});
        
        // popupWindow
        findViewById(R.id.btn_popup_window).setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				View cv = (View)LayoutInflater.from(Activity2.this).inflate(R.layout.popup_window, null);
				final PopupWindow pw = new PopupWindow(cv, 200, 100);
				pw.showAsDropDown(v);
				pw.setFocusable(true);
				pw.update();
				cv.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						System.out.println("onClick ---> dismiss()");
						pw.dismiss();
					}
				});
			}
		});
    }
    
    private ServiceConnection conn = new ServiceConnection() {
		
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			Log.i(TAG, "onServiceDisconnected()");
		}
		
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			Log.i(TAG, "onServiceConnected()");
		}
	};

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity2, menu);
        return true;
    }


	@Override
	protected void onDestroy() {
		Log.i(TAG, "onDestroy()");
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		Log.i(TAG, "onPause()");
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Log.i(TAG, "onRestart()");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.i(TAG, "onResume()");
		super.onResume();
	}

	@Override
	protected void onStart() {
		Log.i(TAG, "onStart()");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.i(TAG, "onStop()");
		super.onStop();
	}

}
